﻿using System;
using ExFlash.Records;

namespace ExFlash.Tags
{
    public class PlaceObjectTag : Tag
    {        
        public UInt16 CharacterId
        {
            get;
            protected set;
        }

        public UInt16 Depth
        {
            get;
            protected set;
        }

        public MatrixRecord TransformMatrix
        {
            get;
            private set;
        }

        public CXFormRecord ColorTransform
        {
            get;
            private set;
        }

        public PlaceObjectTag(int len, Int32 offset)
            : base(TagCodes.PlaceObject, len, offset)
        {

        }

        public PlaceObjectTag(Int32 len, UInt16 charid, UInt16 depth, MatrixRecord mat, Int32 offset)
            : this(len, charid, depth, mat, null, offset)
        {

        }

        public PlaceObjectTag(Int32 len, UInt16 charid, UInt16 depth, MatrixRecord mat, CXFormRecord rec, Int32 offset)
            : base(TagCodes.PlaceObject, len, offset)
        {
            CharacterId = charid;
            Depth = depth;
            TransformMatrix = mat;
            ColorTransform = rec;
        }

        #region ISwfSerializable Members

        public override void SwfDeserialize(BinaryBitReader reader)
        {
            long StartIndex = reader.Position;

            CharacterId = reader.ReadUInt16();

            Depth = reader.ReadUInt16();

            MatrixRecord mat = new MatrixRecord();
            mat.SwfDeserialize(reader);
            TransformMatrix = mat;

            if (reader.Position - StartIndex != TagLength)
            {
                CXFormRecord rec = new CXFormRecord();
                rec.SwfDeserialize(reader);
                ColorTransform = rec;
            }
        }

        public override void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
